package google.sites.model.commands;

import com.google.gdata.util.ServiceException;
import dev.tools.design.patterns.command.CommandException;
import google.sites.model.Model;

/**
 * Created with IntelliJ IDEA.
 * User: Nuno
 * Date: 02-10-2013
 * Time: 18:15
 */
public class Login extends SitesCommand {
    private String authSubToken;
    private String username;
    private String password;

    @Override
    protected void protectedExecute() throws CommandException {
        try {
            if (authSubToken == null)
                getService().setUserCredentials(username, password);
            else
                getService().setAuthSubToken(authSubToken);

            Model.INSTANCE.setState(Model.LOGGED_IN);
        } catch (ServiceException e) {
            throw new CommandException(e);
        }
    }

    public Login setUsername(String username) {
        this.username = username;
        return this;
    }

    public Login setPassword(String password) {
        this.password = password;
        return this;
    }

    public Login setPassword(char[] password) {
        return setPassword(new String(password));
    }

    public Login setAuthSubToken(String authSubToken) {
        this.authSubToken = authSubToken;
        return this;
    }
}
